<?php namespace Home\Controller; 
use Hdphp\Controller\Controller;
use Admin\Model\Goods;
use Admin\Model\Category;
//测试控制器
class ListsController extends Controller{
    private $goods;
    private $category;
	//构造函数
	public function __init(){
        $this->goods = new Goods;
        $this->category = new Category;
	}
	
    //动作
	public function index(){
            //获得cid 
        $cid = Q('get.cid',0,'intval');
        $cateModel = new \Admin\Model\Category;
        $goodsAttrModel = new \Admin\Model\Goods_attr;
            //获取该顶级分类下的所有二级分类
        $two = $cateModel->where("pid={$cid}")->get();
            //如果不是顶级，是二级，则先获取顶级的cid，再获取所有的二级
        if(!$two){
                //获取所点的二级的pid，即顶级的cid
            $pid = $cateModel->where("cid={$cid}")->pluck('pid');
                //获得顶级分类的二级菜单
            $two = $cateModel->where("pid={$pid}")->get();
        }
        View::with('topCate',$two);
            //1.根据分类获得商品的gid
        $gids = $cateModel->cidGetGid($cid);
            //2.根据gids获得商品属性(筛选属性)
        $goodsAttr = $goodsAttrModel->getGoodsAttr($gids);
        View::with('goodsAttr',$goodsAttr);
            //3.筛选地址处理，筛选参数param处理
        $num = count($goodsAttr);
        if($num){
            $param = isset($_GET['param']) ? explode('_',$_GET['param']) :array_fill(0,$num, 0);
        }else{  //如果分类下面没有商品属性的时候
            $param = array();
        }
        View::with('param',$param);
            //4.根据商品属性id进行筛选商品
        $finalGids = $goodsAttrModel->filterGid($param,$gids);
            // 如果没该条件想下的商品则给空数组
        if(!$finalGids){
            $data=array();
        }else{
            //5.通过商品id找到商品
        $finalGids = implode(',',$finalGids);
        $data = $this->goods->where("gid IN ({$finalGids})")->get();
        }
            //面包屑
        $sid=$cid;
        $parent = Data::parentChannel(Db::table("category")->get(), $sid, $fieldPri = 'cid', $fieldPid = 'pid');
        $parents = array();
            //把获得所有父级倒序排列
        $parents = array_reverse($parent);
        View::with('parent',$parents);
            //调取数据
        View::with('data',$data);
            //载入模版
        View::make();
    }

    //筛选
    public function selection(){
        if(IS_POST){
            //得到搜索的内容
            $name = Q('post.search');
            //查数据库，匹配内容
            $data = $this->goods->where('gname','like',"%{$name}%")->get();
            //分配变量
            View::with('name',$name);
            View::with('data',$data);
        }
        View::make();
    }





















}
